package com.zrf.base.knowledge.concurrent.demo;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * 测试call able 结果被缓存的问题
 * @date 2020/3/23
 * @AUTHER zhouruifeng
 */
public class CallableDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        testSingle();

//        testTwo();
    }

    private static void testTwo() throws InterruptedException, ExecutionException {
        Callable<String> callable = new Test1();
        FutureTask<String> futureTask = new FutureTask(callable);
        FutureTask<String> futureTask2 = new FutureTask(callable);

        new Thread(futureTask, "A").start();

        new Thread(futureTask2, "B").start();

        System.out.println(futureTask.get());
        System.out.println(futureTask2.get());
    }

    private static void testSingle() throws InterruptedException, ExecutionException {
        FutureTask<Integer> futureTask = new FutureTask(new Test1());

        new Thread(futureTask, "A").start();

        new Thread(futureTask, "B").start();

        System.out.println(futureTask.get());
        ;

        TimeUnit.SECONDS.sleep(3);
    }

}


class Test1 implements Callable<String> {

    public Test1() { }
    @Override
    public String call() throws Exception {
        System.out.println(Thread.currentThread().getName());
        return "111";
    }
}